<template>
    <div>
        <el-table v-if="gridList"  height="calc(100vh - 280px)" :data="gridList" style="width: 100%;" stripe>
            <el-table-column prop="data_time" label="时间" width="180px"></el-table-column>
            <el-table-column v-for="(item,index) in searchFactor.deviceIds" :label="getPointName(item)" :key="index" align="center">
				<el-table-column  v-for="(it,idx) in titleList" :key="idx" :label="it.phyName+'('+it.unit+')'" >
					<template  slot-scope="scope">
						{{scope.row.pointObj[item]&&scope.row.pointObj[item]['data'+it.lastIdx]}}
					</template>
				</el-table-column>
			</el-table-column>
            <!-- 通过重点监测的删除来控制 -->
            <!-- <el-table-column  v-if="$store.state.permission.buttonPageObj['projectMonitor.delete']" label='操作' width="90px">
                <template  slot-scope="scope">
                <el-button type="text" size="mini" icon="el-icon-delete"  @click.native.prevent="deletePoint(scope.row,scope.$index)">删除</el-button>
                </template>
            </el-table-column> -->
        </el-table>
        <el-pagination v-if="gridList" class="page_sub" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
        :page-sizes="[10,20,30,40,50,60]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
    </div>
</template>
<script>
import { util } from "@/utils/common.js";
export default {
    data(){
        return{
            gridList:null,
            titleList:[],
            page:1,
            size:60,
            total:0,
        }
    },
    props:['searchFactor','pointList'],
    created(){
        this.render();
    },
    methods:{
        getPointName(deviceId){
            let point = this.pointList.find(item=>item.deviceId == deviceId)
            return point.pointName+"("+point.manuId+")";
        },
        handleSizeChange(size){
            this.size = size;
            this.page = 1;
            this.render(this.page)
        },
        handleCurrentChange(page){
            this.page = page;
            this.size = this.size;
            this.render(tis.page)
        },
        render(page){
            this.page = page||1;
            let searchFactor = this.searchFactor,dateTime = ""
            let startTime = searchFactor.modify_start_time||"",
                endTime = searchFactor.modify_end_time||""
            if(startTime){
                startTime = startTime.split(" ").length>1?startTime.replace(' ','T'):(startTime+"T00:00:00")
            }else{
                startTime = '*'
            }
            if(endTime){
                endTime = endTime.split(" ").length>1?endTime.replace(' ','T'):(endTime+"T23:59:59")
            }else{
                endTime = '*'
            }
            if(startTime=="*"&&endTime=="*"){
				dateTime = ""
			}else{
				dateTime = " AND data_time:["+startTime+" TO "+endTime+"]"
			}
            if(this.searchFactor.deviceIds.length ==0){
                this.gridList = []
                return
            }
            let query = ''
            this.searchFactor.deviceIds.forEach(item=>{
                if(!query){
                    query+='data_id:'+item
                }else{
                    query+=' OR data_id:'+item
                }
            })
            query= "("+query+")"+dateTime
            let sendData = {
                query:query,
                pageSize:this.size,
                pageNum:this.page,
                returnType:1
            }
            this.$httpEs.post('/tiesan/queryTiesanTableResult?cancelRequest=true',sendData).then(data=>{
                let titleList  = [];
				let dataMap = data.data.dataMap
				this.gridList =[]
				data.data.timeList.forEach(time=>{
					let pointObj = {}
					dataMap[time].forEach(item=>{
						Object.keys(item).forEach(it=>{
							let lastIdx = it.slice(-1)
							if(util.phyList.includes(it)&&titleList.findIndex(i=>{return i.lastIdx == lastIdx})==-1){
								titleList.push({lastIdx:lastIdx,phyName:item['name'+lastIdx],unit:item['unit'+lastIdx]})
							}
						})
						pointObj[item.data_id] = item;
					})
					this.gridList.push({data_time:time,pointObj:pointObj})
				})
				titleList = titleList.sort((a,b)=>{return a.lastIdx-b.lastIdx})
				this.titleList = [];
				this.$nextTick(()=>{
					this.titleList = titleList
				})
				this.total = data.data.total
            })
        },
        // deletePoint(item,index){
        //     this.$confirm('确定删除吗？', '提示', {
        //         confirmButtonText: '确定',
        //         cancelButtonText: '取消',
        //         type: 'warning'
        //     }).then(() => {
        //     this.$httpEs.post("/point/deleteIndexById",[{_id:item._id}]).then( res=> {
        //             this.$message({
        //                 message: '删除成功',
        //                 type: 'success'
        //             });
        //             this.gridList.splice(index,1)
        //         })
        //     })
        // },
    }
}
</script>